//
// Description: 881. 救生艇
// Created by Loading on 2021/8/26.
//

#include <bits/stdc++.h>

using namespace std;

int numRescueBoats(vector<int> &people, int limit) {
    int res = 0;
    sort(people.begin(), people.end());
    for (int i = 0, j = (int) people.size() - 1; i < j;) {
        // 双指针，尽量使最轻和最重的人在一艘救生艇上
        if (people[i] + people[j] <= limit) {
            ++i;
        }
        --j;
        ++res;
    }

    return res;
}

int main() {
    vector<int> people = {3, 2, 1, 1};
    int limit = 3;
    int res = numRescueBoats(people, limit);
    cout << res << endl;

    return 0;
}